package com.yupi.project.controller;

import com.yupi.project.service.IntegralService;
import com.yupi.project.service.SignInfoService;
import com.yupi.project.service.UserService;
import com.yupi.yuapicommon.model.common.BaseResponse;
import com.yupi.yuapicommon.model.common.BusinessException;
import com.yupi.yuapicommon.model.common.ErrorCode;
import com.yupi.yuapicommon.model.common.ResultUtils;
import com.yupi.yuapicommon.model.entity.User;
import com.yupi.yuapicommon.model.entity.UserInterfaceInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/signInfo")
@Api("签到服务相关")
public class SignInfoController {

    @Resource
    private UserService userService;

    @Resource
    private SignInfoService signInfoService;

    @Resource
    private IntegralService integralService;

    public static final Long SIGN_WOOL = 20L;

    /**
     *
     * @param request
     * @return
     */
    @GetMapping("/sign")
    @ApiOperation("签到")
    public BaseResponse<Object> sign(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        // 防止用户回退页面等操作导致反复签到
        if (signInfoService.isSign(loginUser.getId())) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR, "请勿重复签到");
        }
        Boolean sign = signInfoService.sign(loginUser.getId());
        if(!sign){
            return ResultUtils.error(sign, "签到失败");
        }
        integralService.getWool(SIGN_WOOL, loginUser.getId());
        return ResultUtils.success(SIGN_WOOL);
    }

    /**
     *
     * @param request
     * @return
     */
    @GetMapping("/sign/isSign")
    @ApiOperation("是否签到")
    public BaseResponse<Boolean> isSignToday(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(signInfoService.isSign(loginUser.getId()));
    }

}
